// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

#ifndef HOST_FLASH_INITIALIZATION_H_
#define HOST_FLASH_INITIALIZATION_H_

#include <stdbool.h>
#include "platform.h"
#include "status/rot_status.h"
#include "flash/spi_flash.h"


/**
 * Handler for delayed host flash initialization.
 */
struct host_flash_initialization {
	struct spi_flash *flash_cs0;		/**< SPI interface for CS0 flash. */
	struct flash_master *spi_cs0;		/**< SPI master for CS0 flash. */
	bool is_init0;						/**< Flag indicating if CS0 has been initialized. */
	struct spi_flash *flash_cs1;		/**< SPI interface for CS1 flash. */
	struct flash_master *spi_cs1;		/**< SPI master for CS1 flash. */
	bool is_init1;						/**< Flag indicating if CS1 has been initialized. */
	bool fast_read;						/**< Fast read command flag for flash initialization. */
	bool drive_strength;				/**< Drive strength flag for flash initialization. */
	platform_mutex lock;				/**< Synchronization for flash initialization. */
};


int host_flash_initialization_init (struct host_flash_initialization *init,
	struct spi_flash *flash_cs0, struct flash_master *spi_cs0, struct spi_flash *flash_cs1,
	struct flash_master *spi_cs1, bool fast_read, bool drive_strength);
int host_flash_initialization_init_single_flash (struct host_flash_initialization *init,
	struct spi_flash *flash, struct flash_master *spi, bool fast_read, bool drive_strength);
void host_flash_initialization_release (struct host_flash_initialization *init);

int host_flash_initialization_initialize_flash (struct host_flash_initialization *init);


#define	HOST_FLASH_INIT_ERROR(code)		ROT_ERROR (ROT_MODULE_HOST_FLASH_INIT, code)

/**
 * Error codes that can be generated by a manager for host flash initialization.
 */
enum {
	HOST_FLASH_INIT_INVALID_ARGUMENT = HOST_FLASH_INIT_ERROR (0x00),	/**< Input parameter is null or not valid. */
	HOST_FLASH_INIT_NO_MEMORY = HOST_FLASH_INIT_ERROR (0x01),			/**< Memory allocation failed. */
};


#endif /* HOST_FLASH_INITIALIZATION_H_ */
